<script setup>
import { ref } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'

// 表格数据
const tableData = ref([
  {
    id: 1,
    date: '2023-01-01',
    name: '张三',
    address: '北京市朝阳区',
    status: '正常'
  },
  {
    id: 2,
    date: '2023-01-02',
    name: '李四',
    address: '上海市浦东新区',
    status: '异常'
  },
  {
    id: 3,
    date: '2023-01-03',
    name: '王五',
    address: '广州市天河区',
    status: '正常'
  },
  {
    id: 4,
    date: '2023-01-04',
    name: '赵六',
    address: '深圳市南山区',
    status: '正常'
  },
  {
    id: 5,
    date: '2023-01-05',
    name: '钱七',
    address: '杭州市西湖区',
    status: '异常'
  }
])

// 表格加载状态
const loading = ref(false)

// 搜索条件
const searchForm = ref({
  name: '',
  status: ''
})

// 状态选项
const statusOptions = [
  { label: '全部', value: '' },
  { label: '正常', value: '正常' },
  { label: '异常', value: '异常' }
]

// 分页设置
const currentPage = ref(1)
const pageSize = ref(20)

// 对话框相关
const dialogVisible = ref(false)
const dialogTitle = ref('新增数据')
const formData = ref({
  id: '',
  date: '',
  name: '',
  address: '',
  status: '正常'
})
const isEdit = ref(false)

// 表单规则
const rules = {
  name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
  date: [{ required: true, message: '请选择日期', trigger: 'change' }],
  address: [{ required: true, message: '请输入地址', trigger: 'blur' }],
  status: [{ required: true, message: '请选择状态', trigger: 'change' }]
}

// 搜索方法
const handleSearch = () => {
  loading.value = true
  // 模拟搜索
  setTimeout(() => {
    const filteredData = tableData.value.filter(item => {
      const nameMatch = !searchForm.value.name || item.name.includes(searchForm.value.name)
      const statusMatch = !searchForm.value.status || item.status === searchForm.value.status
      return nameMatch && statusMatch
    })
    tableData.value = filteredData
    loading.value = false
  }, 500)
}

// 重置搜索
const resetSearch = () => {
  searchForm.value = {
    name: '',
    status: ''
  }
  handleSearch()
}

// 打开新增对话框
const handleAdd = () => {
  isEdit.value = false
  dialogTitle.value = '新增数据'
  formData.value = {
    id: '',
    date: '',
    name: '',
    address: '',
    status: '正常'
  }
  dialogVisible.value = true
}

// 打开编辑对话框
const handleEdit = (row) => {
  isEdit.value = true
  dialogTitle.value = '编辑数据'
  formData.value = { ...row }
  dialogVisible.value = true
}

// 删除数据
const handleDelete = (row) => {
  ElMessageBox.confirm('确认删除该条数据吗？', '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    const index = tableData.value.findIndex(item => item.id === row.id)
    if (index > -1) {
      tableData.value.splice(index, 1)
      ElMessage.success('删除成功')
    }
  }).catch(() => {})
}

// 提交表单
const formRef = ref(null)
const handleSubmit = () => {
  if (!formRef.value) return

  formRef.value.validate((valid) => {
    if (valid) {
      if (isEdit.value) {
        // 编辑模式
        const index = tableData.value.findIndex(item => item.id === formData.value.id)
        if (index > -1) {
          tableData.value[index] = { ...formData.value }
          ElMessage.success('修改成功')
        }
      } else {
        // 新增模式
        const newId = Math.max(...tableData.value.map(item => item.id)) + 1
        tableData.value.push({
          ...formData.value,
          id: newId
        })
        ElMessage.success('添加成功')
      }
      dialogVisible.value = false
    }
  })
}
</script>

<template>
  <div class="table-container">
    <h2 class="table-title">数据表格1</h2>

    <!-- 搜索表单 -->
    <div class="search-form">
      <el-form :model="searchForm" inline>
        <el-form-item label="姓名">
          <el-input v-model="searchForm.name" placeholder="请输入姓名" clearable />
        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="searchForm.status" placeholder="请选择状态" clearable>
            <el-option
              v-for="item in statusOptions"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleSearch">查询</el-button>
          <el-button @click="resetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>

    <!-- 工具栏 -->
    <div class="table-toolbar">
      <el-button type="primary" @click="handleAdd">
        <el-icon><Plus /></el-icon> 新增
      </el-button>
    </div>

    <!-- 数据表格 -->
    <el-table
      :data="tableData"
      border
      style="width: 100%"
      v-loading="loading"
    >
      <el-table-column prop="id" label="ID" width="80" />
      <el-table-column prop="date" label="日期" width="120" />
      <el-table-column prop="name" label="姓名" width="120" />
      <el-table-column prop="address" label="地址" />
      <el-table-column prop="status" label="状态" width="120">
        <template #default="{ row }">
          <el-tag :type="row.status === '正常' ? 'success' : 'danger'">
            {{ row.status }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="180" fixed="right">
        <template #default="{ row }">
          <el-button size="small" type="primary" @click="handleEdit(row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <div class="pagination-container">
      <el-pagination
        layout="total, sizes, prev, pager, next, jumper"
        :total="100"
        v-model:current-page="currentPage"
        v-model:page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
      />
    </div>

    <!-- 新增/编辑对话框 -->
    <el-dialog
      v-model="dialogVisible"
      :title="dialogTitle"
      width="500px"
      destroy-on-close
    >
      <el-form
        ref="formRef"
        :model="formData"
        :rules="rules"
        label-width="80px"
      >
        <el-form-item label="日期" prop="date">
          <el-date-picker
            v-model="formData.date"
            type="date"
            placeholder="请选择日期"
            value-format="YYYY-MM-DD"
            style="width: 100%"
          />
        </el-form-item>
        <el-form-item label="姓名" prop="name">
          <el-input v-model="formData.name" placeholder="请输入姓名" />
        </el-form-item>
        <el-form-item label="地址" prop="address">
          <el-input v-model="formData.address" placeholder="请输入地址" />
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <el-select v-model="formData.status" placeholder="请选择状态" style="width: 100%">
            <el-option label="正常" value="正常" />
            <el-option label="异常" value="异常" />
          </el-select>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="handleSubmit">确定</el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>
.table-container {
  padding: 15px;
  background-color: #fff;
  height: 100%;
  overflow: auto;
  display: flex;
  flex-direction: column;
}

.table-title {
  font-size: 18px;
  margin-bottom: 15px;
  font-weight: 500;
  color: #303133;
}

.search-form {
  margin-bottom: 15px;
  padding-bottom: 15px;
  border-bottom: 1px solid #ebeef5;
}

.table-toolbar {
  margin-bottom: 15px;
}

.el-table {
  flex: 1;
}

.pagination-container {
  margin-top: 15px;
  text-align: right;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}
</style>
